(function (mui, $) {
var page = window.page = {};

// 普通页面跳转
$('body').delegate('[data-url]', 'tap', function () {
	app.open($(this).attr('data-url'));
});

mui('.mui-slider').slider({interval:5000});

function selectTab(tab) {
  switch (tab) {
    case '1':
    $('.mui-tab-item:eq(1)').trigger('tap');
    break;
    case '2':
    $('.mui-tab-item:eq(2)').trigger('tap');
    break;
    case '3':
    $('.mui-tab-item:eq(3)').trigger('tap');
    break;
    case '4':
    $('.mui-tab-item:eq(4)').trigger('tap');
    break;
    default:
    $('.mui-tab-item:eq(0)').trigger('tap');
  }
}

function next (cb) {
	if (plus.webview.currentWebview().isVisible()) { plus.nativeUI.showWaiting(); }
	$.ajax({
		'dataType' : 'json',
		'type'     : 'post',
		'url'      : app.url('App/Index/Index/index'),
		'data'     : {},
	})
	.fail(function (res) {
		// 失败回调
		plus.nativeUI.closeWaiting();
		app.log('取得首页数据失败：', res);
		app.toast('取得首页数据失败');
		app.tryResetPullLoading();
	})
	.done(function (res) {
		// 成功回调
		plus.nativeUI.closeWaiting();
		app.log('首页数据：', res);
		
		if (500 == res.code) { app.toast('你还未登录'); app.tryResetPullLoading(); return; }
		if (300 == res.code) { app.toast(res.msg); return; }
		if (200 != res.code) { app.toast('请求失败'); return; }
		if (res.msg && plus.webview.currentWebview().isVisible()) { app.toast(res.msg); }
		
		cb && cb(res);
	})
	;
};

function call_on_pull_down () {
	var pullrefresh = mui('.container-pull-refresh').pullRefresh();
	
	next (function (res) {
		$('.app-name').text(res.data.name);
		// 轮播图推荐
		mui('.mui-slider').slider('destroy');
		$('.mui-slider').html(app.tpl('tpl-banner-slider', res));
		// 推荐分类
		$('.featured-categories').html(app.tpl('tpl-category-row', res));
		// 广告内容
		$('#ad-area').html(app.tpl('tpl-ad', res));
		// 热门商品
		$('.hot-products').html(app.tpl('tpl-hot-product', res));
		// 热门专题
		$('.hot-topics').html(app.tpl('tpl-hot-topic', res));
		// 热门活动
		$('.hot-activities').html(app.tpl('tpl-hot-activity', res));
		// 动态设定热门活动居中 采用相对定位margin来做居中，会看不到灰背景色
		var width = $('.activity:first').width();
		$('.hot-activities .activity .word').css('left', function () {
			return (width - $(this).width()) / 2;
		});
		
		app.store('home', res);
		mui('.mui-slider').slider({interval:5000});
		pullrefresh.endPulldownToRefresh(); 

    selectTab(app.getQueryString('tab'));
	});
}
function call_on_pull_up () {
	var pullrefresh = mui('.container-pull-refresh').pullRefresh();
	
	next (function (res) {
		var dom = $(pullrefresh.element).find('.mui-scroll>.scroll-data');
		$(dom).append(app.tpl($(dom).attr('data-tpl'), res));
		pullrefresh.endPullupToRefresh();
	});
}

mui.plusReady(function () {
  call_on_pull_down();
});

$('.mui-tab-item').each(function (index, dom) {
  var length = $('.mui-tab-item').size();
  $('.tab-' + index).css('z-index', length - index - 1);
  $(dom).on('tap', function () {
    app.clearHis();
    app.putHis(app.link.weixin + '/html/home.html?tab=' + index);
    $(this).siblings().removeClass('mui-active');
    $(this).addClass('mui-active');
    $('.tab-' + index).siblings().each(function (i, d) {
      $(d).css('z-index', length - i - 2);
    });
    $('.tab-' + index).css('z-index', length - 1);
  });
});

// 轮播图跳转到专题详情
$('body').delegate('[data-type=go_special]', 'tap', function (e) {
	e.stopPropagation();
	app.open('topic.detail.html', {'id':$(this).attr('data-value')});
});
// 轮播图跳转到活动详情
$('body').delegate('[data-type=go_activity]', 'tap', function (e) {
	e.stopPropagation();
	app.open('activity.detail.html', {'id':$(this).attr('data-value')});
});
// 轮播图跳转到指定分类的商品列表
$('body').delegate('[data-type=go_category]', 'tap', function (e) {
	e.stopPropagation();
	app.open('product.search.html', {'category_id':$(this).attr('data-value')});
});
// 轮播图跳转到指定商品详情页面
$('body').delegate('[data-type=go_goods]', 'tap', function (e) {
	e.stopPropagation();
	app.open('product.detail.html', {'id':$(this).attr('data-value')});
});
// 轮播图跳转到外部页面
$('body').delegate('[data-type=go_page]', 'tap', function (e) {
	e.stopPropagation();
	app.open('app.outer.html', {'url':$(this).attr('data-value')});
});

// 跳转到热门分类
$('body').delegate('.featured-categories .category', 'tap', function (e) {
	e.stopPropagation();
	var dom = this, id = $(this).attr('data-id');
	app.open('product.search.html', {'category_id':id});
});
// 更多推荐分类
$('body').delegate('.more-category', 'tap', function () {
  selectTab('1');
	plus.webview.getLaunchWebview().evalJS('page.tab("category.html")');
});
// 热门商品
$('body').delegate('.hot-products .hot-product', 'tap', function (e) {
	e.stopPropagation();
	app.open('product.detail.html', {'id':$(this).attr('data-id')});
});
// 点赞热门商品
$('body').delegate('.hot-products .btn-awesome', 'tap', function (e) {
	e.stopPropagation();
	app.toast('点赞成功,程序开发中');
});
// 热门专题
$('body').delegate('.topic', 'tap', function (e) {
	e.stopPropagation();
	app.open('topic.detail.html', {'id':$(this).attr('data-id')});
});
// 热门活动
$('body').delegate('.hot-activities .activity', 'tap', function (e) {
	e.stopPropagation();
	app.open('activity.detail.html', {'id':$(this).attr('data-id')});
});

// 查看全部
$('body').delegate('.btn-view-all', 'tap', function () {
  var dom = this, id = $(this).attr('data-id');
  app.open('product.search.html');
});

})(mui, $);
